package com.link.base.base.product.controller;

import com.link.base.base.product.model.PriceList;
import com.link.base.base.product.service.PriceListService;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.controller.BasicControllerException;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.util.UserUtil;
import com.link.core.cllog.LCLogger;
import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 价格表头
 * @author hhx
 * @date 2016年9月22日
 */
@Controller
@RequestMapping("/link/priceList")
public class PriceListController extends BasicController<PriceList> {

    @Resource
    private PriceListService priceListService;

    @Resource
    private KeyGenerateService keyGenerateService;


    @Override
    public BasicService<PriceList> getBasicService() throws Exception {
        return priceListService;
    }


    /**
     * 新建默认值
     */
    @Override
    @RequestMapping(value = "/preDefaultValue", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> preDefaultValue(@JsonParam PriceList entity, HttpSession session, HttpServletRequest request,
                                               HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            //新建默认值赋值
            CoreUser user = UserUtil.getUser();
            PriceList priceList = new PriceList();
            priceList.setId(keyGenerateService.keyGenerate());
            priceList.setOrgId(user.getOrgId());
            priceList.setOrgName(user.getOrgName());
            SimpleDateFormat dfFormat = new SimpleDateFormat("yyyy-MM-dd-yyyy HH:mm:ss");
            Date myDate = new Date();
            String sysdate = dfFormat.format(myDate);
            priceList.setEffStartDate(sysdate);
            priceList.setActivated("N");
            result.put("success", true);
            result.put("result", priceList);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 查找一个价格表信息
     */
    @RequestMapping(value = "/queryOnePriceList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryOnePriceList(PriceList record, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        PriceList priceList = new PriceList();
        try {
            priceList = priceListService.queryById(record);
            result.put("success", true);
            result.put("result", priceList);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        result.put("priceList", priceList);
        return result;
    }

    /**
     * 查找一个组织下的所有价格表
     */
    @RequestMapping(value = "/queryByOrgId")
    @ResponseBody
    public List<PriceList> priceList(Long orgId, @JsonParam QueryParams qps, HttpSession session,
                                     HttpServletRequest request, HttpServletResponse response) {
        List<PriceList> list = null;
        PriceList record = new PriceList();
        //组织ID赋值
        record.setOrgId(orgId);
        try {
            record.setPageFlag(false);
            list = priceListService.queryByExamplePage(record);
        } catch (Exception e) {
            LCLogger.withException(e);
        }
        return list;
    }

    /**
     * 插入或更新前做操作
     */
    @Override
    public void beforUpsert(PriceList entity, HttpServletRequest request) throws Exception {
        SimpleDateFormat dfFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date sDate2 = dfFormat.parse(entity.getEffStartDate());
        //有效期结束时间不为空
        if (entity.getEffEndDate() != null && !"".equals(entity.getEffEndDate())) {
            Date eDate2 = dfFormat.parse(entity.getEffEndDate());
            //判断有效期开始时间和有效期结束时间大小关系
            if (eDate2.getTime() < sDate2.getTime()) {
                throw new BasicControllerException("价格表的有效结束时间不能比有效开始时间早，请重新编辑！");
            }
        }
    }

    /**
     * picklist查询000公司帐套信息
     */
    @RequestMapping(value = "/queryStandardPage")
    @ResponseBody
    public Map<String, Object> queryStandardPage(@JsonParam QueryParams qps, @JsonParam PriceList entity, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            qps.preDealSecurity(request);
            PriceList t = (PriceList) BasicModel.transformClass(entity.getClass(), qps);
            beforQueryAllOrExample(t, request);
            beforQueryExample(t, request);
            t.setLoginCorpId(0L);
            List<PriceList> list = getBasicService().queryByExamplePage(t);
            result.put("success", true);
            result.put("rows", list);
            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<PriceList>());
            result.put("result", e.getMessage());
        }
        return result;
    }
}
